package com.qiuhui.controller;

import com.qiuhui.entity.Poetry;
import com.qiuhui.service.PoetryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @ClassName: PoetryController
 * @Description: TODO
 * @author: tian
 * @date: 2021/3/24  6:20 下午
 */
@Controller
@RequestMapping("/poetry")
public class PoetryController {
    @Autowired
    @Qualifier("poetryServiceImpl")
    private PoetryService poetryService;

    @RequestMapping("/all")
    public String getPoe(Model model) {
        List<Poetry> list = poetryService.getPoetryByList();
        model.addAttribute("list", list);
        return "allPoetry";
    }

    @RequestMapping("/toAddPoetry")
    public String toAddPoe() {
        return "addPoetry";
    }

    @RequestMapping("addPoetry")
    public String addPoetry(Poetry poetry) {
        poetryService.addPoe(poetry);
        return "redirect:/poetry/all";
    }

    @RequestMapping("toUpdatePoetry")
    public String toUpdatePoe(Model model, int id) {
        Poetry poetry = poetryService.getPoe(id);
        model.addAttribute("poetry", poetry);
        return "updatePoetry";
    }

    @RequestMapping("updatePoetry")
    public String updatePoetry(Model model, Poetry poetry) {
        poetryService.updatePoe(poetry);
        Poetry poe = poetryService.getPoe(poetry.getId());
        model.addAttribute("poetry", poe);
        return "redirect:/poetry/all";
    }

    @RequestMapping("/toDelPoetry/{poetryId}")
    public String toDelPoe(@PathVariable("poetryId") int id) {
        poetryService.delPoe(id);
        return "redirect:/poetry/all";
    }
}
